package com.raysdata.smartcity.flow.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;

import java.util.List;

/**
 * Created by zhoukai on 2016/11/18.
 */
public class ProcessDef {
    private static final String BPMN_FILE_BASE_PATH = "flow/activiti/bpmn/";
    private static final String BPMN_FILE_SUBFIX = ".bpmn20.xml";
    /**
     * 部署流程定义
     * @param processEngine
     * @param pdCatalog - 目录
     * @param pdName - 名字
     * @param bpmnFile - 流程定义文件路径(flow/activiti/bpmn/*.bpmn)
     * @return
     */
    public static String deployPD(ProcessEngine processEngine,String pdCatalog,String pdName,String bpmnFile){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().category(pdCatalog).name(pdName).addClasspathResource(BPMN_FILE_BASE_PATH + bpmnFile + BPMN_FILE_SUBFIX).deploy();
        return deployment.getId();
    }

    /**
     * 删除流程部署（级联删除,删除和当前规则相关的所有信息，包括历史）
     * @param processEngine
     * @param pdId
     */
    public static void delProcessDeployById(ProcessEngine processEngine,String deployId){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.deleteDeployment(deployId,true);
    }

    /**
     * 获得所有流程定义
     * @param processEngine
     * @return
     */
    public static List<Deployment> findAllPd(ProcessEngine processEngine){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        return repositoryService.createDeploymentQuery().list();
    }

    /**
     * 根据流程目录获得流程定义
     * @param processEngine
     * @param catalogName - 目录名称
     * @return
     */
    public static List<Deployment> findPdByCatalog(ProcessEngine processEngine,String catalogName){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        return repositoryService.createDeploymentQuery().deploymentCategory(catalogName).list();
    }
}
